package org.example;

import javax.swing.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IO_Demo23 {
    public static void main(String[] args) throws IOException {
        //爬取
        String familyNameNet="https://www.baidu.com/s?ie=utf-8&wd=姓氏";
        String boyNameNet="https://www.baidu.com/s?ie=utf-8";
        String girlNameNet="https://www.baidu.com";

        //爬取姓氏
        String familyName=webCrawl(familyNameNet);
        System.out.println(familyName);
        //爬取男孩名
        String boyName=webCrawl(boyNameNet);
        System.out.println(boyName);
        //爬取女孩名
        String girlName=webCrawl(girlNameNet);
        System.out.println(girlName);
        //通过正则表达式获取数据
        //姓氏
        ArrayList<String> familytempNameList = getDate(familyName, "(.{4})(，|。)", 1);
        //男孩名
        ArrayList<String> boytempNameList = getDate(boyName, "/^(?:[\\u4e00-\\u9fa5·]{2,16})$/", 1);
        //女孩名
        ArrayList<String> girltempNameList = getDate(girlName, "(..){4}..", 0);


        //生成数据
        ArrayList<String> list = getInfos(familytempNameList, boytempNameList, girltempNameList, 100, 100);
        System.out.println(list);

        //写出数据
        BufferedWriter br=new BufferedWriter(new FileWriter("D:\\文件名"));
        for (String s : list) {
            br.write(s);
            br.newLine();
        }
        br.close();



        //处理姓氏
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String str : familytempNameList) {
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                familyNameList.add(ch + "");
            }
        }
        //处理男名
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String str : boyNameList) {
            if(!boyNameList.contains(str)){
                boyNameList.add(str);
            }
        }
        //处理女名
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String str : girlNameList) {
            String[] arr=str.split(" ");
            for (int i = 0; i < arr.length; i++) {
                girlNameList.add(arr[i]);

            }
        }

    }

    public static ArrayList<String> getInfos( ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList,int boyCount,int girlCount){
      //男生名字
        HashSet<String> boyhs = new HashSet<String>();
        while(true){
          if(boyhs.size()==boyCount){
              break;
          }
          //打乱姓氏和男孩名
          Collections.shuffle(familyNameList);
          Collections.shuffle(boyNameList);
          //拼接第一个姓氏和第一个男孩名
          boyhs.add(familyNameList.get(0)+boyNameList.get(0));
        }
      //女生名字
        HashSet<String> girlhs = new HashSet<String>();
        while(true){
          if(girlhs.size()==girlCount){
              break;
          }
          Collections.shuffle(familyNameList);
          Collections.shuffle(girlNameList);
          girlhs.add(familyNameList.get(0)+girlNameList.get(0));
        }
        ArrayList<String> list=new ArrayList<>();
        Random r =new Random();
        for (String boyname : boyNameList) {
            //年龄18~27岁
            int age = r.nextInt(10) + 18;
            list.add(boyname+"-"+age);
        }
        for (String girlname : girlNameList) {
            //年龄18~25岁
            int age = r.nextInt(8) + 18;
            list.add(girlname+"-"+age);
        }
        return list;
    }

    private static ArrayList<String> getDate(String str, String s, int i) {
        //超级爱你集合放数据
        ArrayList<String> list = new ArrayList<>();
        //按照正则表达式规则获取数据
        Pattern p = Pattern.compile(s);
        //按照pattern规则到str中获取数据
        Matcher m = p.matcher(str);
        //判断是否有数据
        while(m.find()){
            //获取数据
            String s1 = m.group(i);
            //添加到集合
            list.add(s1);
        }
        return list;
    }


    public static String webCrawl(String net) throws IOException {
        //定义StringBuilder拼接爬取到的数据
        StringBuilder sb = new StringBuilder();
        // 1. 创建一个 URL 对象
        URL url = new URL(net);
        // 2. 链接上这个网址（网址必须是畅通的）
        URLConnection conn =  url.openConnection();
        //读取数据
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        int ch;
        while((ch=isr.read())!=-1){
            sb.append((char)ch);
        }
        //释放资源
        isr.close();
        //返回所读取的数据
        return sb.toString();
    }





}
